﻿using System;
using System.IO;
using System.Text;
using System.Threading;
using HslCommunication.Core;

namespace HslCommunication.BasicFramework
{
	/// <summary>
	/// 一个用于自动流水号生成的类，必须指定保存的文件，实时保存来确认安全
	/// </summary>
	/// <remarks>
	/// <note type="important">
	/// 序号生成器软件，当获取序列号，清空序列号操作后，会自动的将ID号存储到本地的文件中，存储方式采用乐观并发模型实现。
	/// </note>
	/// </remarks>
	/// <example>
	/// 此处举个例子，也是Demo程序的源代码，包含了2个按钮的示例和瞬间调用100万次的性能示例。
	/// <note type="tip">百万次调用的实际耗时取决于计算机的性能，不同的计算机的表现存在差异，比如作者的：i5-4590cpu,内存ddr3-8G表示差不多在800毫秒左右</note>
	/// <code lang="cs" source="TestProject\HslCommunicationDemo\FormSeqCreate.cs" region="FormSeqCreate" title="示例代码" />
	/// </example>
	// Token: 0x02000197 RID: 407
	public sealed class SoftNumericalOrder : SoftFileSaveBase
	{
		/// <summary>
		/// 实例化一个流水号生成的对象
		/// </summary>
		/// <param name="textHead">流水号的头文本</param>
		/// <param name="timeFormate">流水号带的时间信息</param>
		/// <param name="numberLength">流水号数字的标准长度，不够补0</param>
		/// <param name="fileSavePath">流水号存储的文本位置</param>
		// Token: 0x060020E6 RID: 8422 RVA: 0x0009FDFC File Offset: 0x0009DFFC
		public SoftNumericalOrder(string textHead, string timeFormate, int numberLength, string fileSavePath)
		{
			base.LogHeaderText = "SoftNumericalOrder";
			this.TextHead = textHead;
			this.TimeFormate = timeFormate;
			this.NumberLength = numberLength;
			base.FileSavePath = fileSavePath;
			this.LoadByFile();
			this.AsyncCoordinator = new HslAsyncCoordinator(delegate()
			{
				bool flag = !string.IsNullOrEmpty(base.FileSavePath);
				if (flag)
				{
					using (StreamWriter streamWriter = new StreamWriter(base.FileSavePath, false, Encoding.Default))
					{
						streamWriter.Write(this.CurrentIndex);
					}
				}
			});
		}

		/// <summary>
		/// 获取流水号的值
		/// </summary>
		/// <returns>字符串信息</returns>
		// Token: 0x060020E7 RID: 8423 RVA: 0x0009FE88 File Offset: 0x0009E088
		public override string ToSaveString()
		{
			return this.CurrentIndex.ToString();
		}

		/// <summary>
		/// 加载流水号
		/// </summary>
		/// <param name="content">源字符串信息</param>
		// Token: 0x060020E8 RID: 8424 RVA: 0x0009FEA5 File Offset: 0x0009E0A5
		public override void LoadByString(string content)
		{
			this.CurrentIndex = Convert.ToInt64(content);
		}

		/// <summary>
		/// 清除流水号计数，进行重新计数
		/// </summary>
		// Token: 0x060020E9 RID: 8425 RVA: 0x0009FEB4 File Offset: 0x0009E0B4
		public void ClearNumericalOrder()
		{
			Interlocked.Exchange(ref this.CurrentIndex, 0L);
			this.AsyncCoordinator.StartOperaterInfomation();
		}

		/// <summary>
		/// 获取流水号数据
		/// </summary>
		/// <returns>新增计数后的信息</returns>
		// Token: 0x060020EA RID: 8426 RVA: 0x0009FED4 File Offset: 0x0009E0D4
		public string GetNumericalOrder()
		{
			long num = Interlocked.Increment(ref this.CurrentIndex);
			this.AsyncCoordinator.StartOperaterInfomation();
			bool flag = string.IsNullOrEmpty(this.TimeFormate);
			string result;
			if (flag)
			{
				result = this.TextHead + num.ToString().PadLeft(this.NumberLength, '0');
			}
			else
			{
				result = this.TextHead + DateTime.Now.ToString(this.TimeFormate) + num.ToString().PadLeft(this.NumberLength, '0');
			}
			return result;
		}

		/// <summary>
		/// 获取流水号数据
		/// </summary>
		/// <param name="textHead">指定一个新的文本头</param>
		/// <returns>带头信息的计数后的信息</returns>
		// Token: 0x060020EB RID: 8427 RVA: 0x0009FF64 File Offset: 0x0009E164
		public string GetNumericalOrder(string textHead)
		{
			long num = Interlocked.Increment(ref this.CurrentIndex);
			this.AsyncCoordinator.StartOperaterInfomation();
			bool flag = string.IsNullOrEmpty(this.TimeFormate);
			string result;
			if (flag)
			{
				result = textHead + num.ToString().PadLeft(this.NumberLength, '0');
			}
			else
			{
				result = textHead + DateTime.Now.ToString(this.TimeFormate) + num.ToString().PadLeft(this.NumberLength, '0');
			}
			return result;
		}

		/// <summary>
		/// 单纯的获取数字形式的流水号
		/// </summary>
		/// <returns>新增计数后的信息</returns>
		// Token: 0x060020EC RID: 8428 RVA: 0x0009FFEC File Offset: 0x0009E1EC
		public long GetLongOrder()
		{
			long result = Interlocked.Increment(ref this.CurrentIndex);
			this.AsyncCoordinator.StartOperaterInfomation();
			return result;
		}

		/// <summary>
		/// 当前的生成序列号
		/// </summary>
		// Token: 0x040007CD RID: 1997
		private long CurrentIndex = 0L;

		/// <summary>
		/// 流水号的文本头
		/// </summary>
		// Token: 0x040007CE RID: 1998
		private string TextHead = string.Empty;

		/// <summary>
		/// 时间格式默认年月日
		/// </summary>
		// Token: 0x040007CF RID: 1999
		private string TimeFormate = "yyyyMMdd";

		/// <summary>
		/// 流水号数字应该显示的长度
		/// </summary>
		// Token: 0x040007D0 RID: 2000
		private int NumberLength = 5;

		/// <summary>
		/// 高性能存储块
		/// </summary>
		// Token: 0x040007D1 RID: 2001
		private HslAsyncCoordinator AsyncCoordinator = null;
	}
}
